<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //格式化日期：yyyy-MM-dd

      function formatDate(date) {
        var myyear = date.getFullYear();
        var mymonth = date.getMonth() + 1;
        var myweekday = date.getDate();
        if (mymonth < 10) {
          mymonth = "0" + mymonth;
        }
        if (myweekday < 10) {
          myweekday = "0" + myweekday;
        }
        return myyear + "-" + mymonth + "-" + myweekday;
      }
      //获得某月的天数
      function getMonthDays(paraYear, paraMonth) {
        var monthStartDate = new Date(paraYear, paraMonth, 1);
        var monthEndDate = new Date(paraYear, paraMonth + 1, 1);
        var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
        return days;
      }
      //获得某周的开始日期
      function getWeekStartDate(paraYear, paraMonth, paraDay, paraDayOfWeek) {
        var weekStartDate = new Date(
          paraYear,
          paraMonth,
          paraDay + 1 - paraDayOfWeek
        );
        return formatDate(weekStartDate);
      }
      //获得某周的结束日期
      function getWeekEndDate(paraYear, paraMonth, paraDay, paraDayOfWeek) {
        var weekEndDate = new Date(
          paraYear,
          paraMonth,
          paraDay + (7 - paraDayOfWeek)
        );
        return formatDate(weekEndDate);
      }
      //获得某月的开始日期
      function getMonthStartDate(paraYear, paraMonth) {
        var monthStartDate = new Date(paraYear, paraMonth, 1);
        return formatDate(monthStartDate);
      }
      //获得某月的结束日期
      function getMonthEndDate(paraYear, paraMonth) {
        var monthEndDate = new Date(
          paraYear,
          paraMonth,
          getMonthDays(paraYear, paraMonth)
        );
        return formatDate(monthEndDate);
      }
      console.log(getMonthStartDate(2020, 1 - 1));
      console.log(getMonthEndDate(2020, 05 - 1));
    </script>
  </body>
</html>
